﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Skynet.BusinessEntities;
using System.Data;
namespace Skynet.BusinessLogic.DataAccess
{
    public class AlternativeExpEvaluationRepository:  MasterRepository<BusinessEntities.AlternativeExpEvaluation>, IAlternativeExpEvaluationRepository
    {
        public AlternativeExpEvaluationRepository(IUnityContainer container)
            : base(container)
        { 
        }        
        public void Maintenance(BusinessEntities.AlternativeExpEvaluation item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            try
            {                
                ctx.AlternativeExpEvaluations.ApplyChanges(item);
                ctx.SaveChanges();
                item.AcceptChanges();
            }
            catch (OptimisticConcurrencyException ex)
            {
                ctx.Refresh(refreshMode: System.Data.Objects.RefreshMode.StoreWins, entity: item);
            }
            catch (Exception)
            {

                throw;
            }
            
        }

        public override AlternativeExpEvaluation GetOne(object key)
        {        
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = from c in ctx.AlternativeExpEvaluations where c.EvaluationKey == (string)key select c;
            return query.FirstOrDefault();
        
        }
        public string GetEvaluationExpression(string AlternativeExpKey)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.AlternativeExpEvaluations where c.EvaluationKey == AlternativeExpKey select c).SingleOrDefault();

            return query ==null? "" :query.EvaluationExp;
        }
    }
}
